<template>
    <div class="">
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item><i class="el-icon-message"></i>&nbsp;新增分站</el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div style="max-width:1000px;padding:10px 0 30px 0px">
                <el-alert
                    title="建议上传的图片压缩之后再上传，便于APP请求图片资源加载速度快"
                    type="warning"
                    show-icon>
                </el-alert>
            </div>
            <el-form ref="form" label-width="160px">
                <el-form-item label="分站LOGO：">
                    <div class="sola-upload clearfix">
                        <div class="sola-left clearfix" v-show="logo!=''">
                            <div  class="sola-flex" >
                                <img  :src="logo" />
                                <span class="delete" @click="delPicture()"><i class="el-alert__icon el-icon-error"></i></span>
                            </div>
                        </div>
                        <div class="sola-file-box">
                            <el-button type="primary" plain>上传图片<i class="el-icon-upload el-icon--right"></i></el-button>
                            <input class="btn-file" type="file" @change="uploadBanner" v-loading.fullscreen.lock="fullscreenLoading"/>
                            <span style="color:red;padding-left:10px;">建议上传的轮播图比例是1：1 示例100X100 </span>
                        </div>
                    </div>
                </el-form-item>
                <el-form-item label="分站名称：">
                    <el-input v-model="name" placeholder="请输入分站名称" style="width:600px"></el-input>
                </el-form-item>
                <el-form-item label="站长联系电话：">
                    <el-input v-model="contact	" type="tel" placeholder="请输入站长联系电话" style="width:600px"></el-input>
                </el-form-item>
                 <el-form-item label="详细地址：">
                    <el-input v-model="address	" type="tel" placeholder="请输入详细地址" style="width:600px"></el-input>
                </el-form-item>
                <el-form-item label="经度：">
                    <el-input v-model="latitude" type="tel" placeholder="请输入所在地址经度" style="width:600px"></el-input>
                </el-form-item>
                  <el-form-item label="维度：">
                    <el-input v-model="longitude" type="tel" placeholder="请输入所在地址维度" style="width:600px"></el-input>
                </el-form-item>
                <el-form-item label="经度获取方式" style="width:1000px">
                    示例：腾讯地图 坐标器 （提示：经纬度的,标点符号是分割线；标点前面是经度，后面是维度，请复制经纬度数字进来，不要带上,标点符号）<a href="http://lbs.qq.com/tool/getpoint/index.html"  target="_blank">跳转查看</a>
                    <img src="http://appimg.yinsoso.com/backend/TIM%E6%88%AA%E5%9B%BE20190320134958.png" width="1000px;">
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="onAddGoods">确认发布</el-button>
                </el-form-item>
            </el-form>
        </div>
        <!-- 删除提示框 -->
        <el-dialog title="提示" :visible.sync="delVisible" width="300px" center>
            <div class="del-dialog-cnt">删除不可恢复，是否确定删除？</div>
            <span slot="footer" class="dialog-footer">
                <el-button @click="delVisible = false">取 消</el-button>
                <el-button type="primary" @click="deleteRow">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>
<script>
import {postUploadBase64} from '@/api/public'
import {postAddSubstation} from '@/api/substation'
export default {
    name:"SchoolAdd",
    data() {
        return {
            fullscreenLoading:false,
            delVisible:false,
            accept: 'image/gif, image/jpeg, image/png, image/jpg', //图片上传类型
            name:'',//分站名称
            logo:'',//分站LOGO
            contact:'',//站长联系电话
            address:'',//地址
            longitude:'',//经度
            latitude:'',//维度
        }
    },
    //实例创建
    created(){
        
    },
    //计算属性
    computed:{

    },
    //方法
    methods: {
        //确定删除图片
        deleteRow(){
            this.logo="";
            this.$message.success('删除成功');
            this.delVisible = false;
        },
        //删除图片
        delPicture(){
            //当前记录的下标
            this.delVisible = true;
        },
        //触发项目封面图 file
        uploadBanner(event){
            var _event = event;
            var _this = this;
            let reader =new FileReader();  
            let img1=event.target.files[0];  
            let type=img1.type;//文件的类型，判断是否是图片  
            let size=img1.size;//文件的大小，判断图片的大小
            const files = event.target;  
            if(this.accept.indexOf(type) == -1){  
                this.$message({
                    message: '请选择gif,jpeg,png,jpg格式图片上传',
                    type: 'warning'
                });
                return false;  
            }  
            else if(size>3145728){  
                this.$message({
                    message: '请选择3M以内的图片上传',
                    type: 'warning'
                }); 
                return false;  
            }else if (typeof(FileReader) === 'undefined') {
                this.$notify.error({
                    title: '错误',
                    message: '抱歉，你的浏览器不支持 FileReader，请使用现代浏览器操作！'
                });

            }else {
                this.fullscreenLoading = true;
                let reader = new FileReader();
                reader.readAsDataURL(img1);
                reader.onload = function(e) {
                    var base = this.result;
                    // console.log(base);
                    
                    postUploadBase64(base).then(response=>{
                        // console.log(response);
                        if(response.code==200){
                            _this.$message({
                                message: '恭喜你，图片上传成功',
                                type: 'success'
                            });
                            _this.logo=response.data;
                            
                        }else{
                            _this.$notify.error({
                                title: '上传图片失败',
                                duration:2000,
                                message: '失败原因：'+response.msg
                            });
                        }
                        _this.fullscreenLoading = false;
                    })

                }
                
            }
        },
        //确认新增商品
        onAddGoods(){
            if(this.logo==""){
                this.$notify.info({title: '提示',message: '请先上传分站LOGO'});
                return false;
            }
            else if(this.name==""){
                this.$notify.info({title: '提示',message: '请先填写分站名称'});
                return false;
            }
            else if(this.contact == '' || !(/(1[3-9]\d{9}$)/.test(this.contact))){
                 this.$notify.info({title: '提示',message: '请先正确填写站长联系电话'});
                return false;
            }
            else if(this.address == ''){
                    this.$notify.info({title: '提示',message: '请先填写详细地址'});
                return false;
            }
            else if(this.longitude == ''){
                    this.$notify.info({title: '提示',message: '请先填写所在地址经度'});
                return false;
            }
            else if(this.latitude == ''){
                    this.$notify.info({title: '提示',message: '请先填写所在地址维度'});
                return false;
            }
            else{
                let param ={}
                param.name=this.name,
                param.logo=this.logo,
                param.contact=this.contact,
                param.address=this.address,
                param.longitude=this.longitude,
                param.latitude=this.latitude,
                //调用提交方法
                this.saveSubstation(param);
            }
        },
        //提交新增分站
        saveSubstation(param){
            var _this =this;
            postAddSubstation(param).then(response=>{
                //console.log(response);
                if(response.code==200){
                    this.$message({
                        message: '恭喜你，发布成功',
                        type: 'success'
                    });
                    setTimeout(function(){
                        _this.$router.push('/substation');
                    },1500);

                }else{
                    this.$notify.error({
                        title: '发布失败',
                        duration:2000,
                        message: '失败原因：'+response.msg
                    });
                }

            }).catch(error=>{
                console.log(error);
            });
        }
    }
}
</script>
<style lang="stylus" scoped>
    .el-form-item .el-cascader{
        width :400px;
    }
    .el-cascader-menus .el-cascader-menu{
        width :240px
    }
    .sola-upload{
        .sola-left{
            width :100%;
            height :auto;
            .sola-flex{
                float:left
                margin-right:15px;
                position:relative;
                .delete{
                    position:absolute;
                    top:2px;
                    right:13px;
                    color: #f56c6c;
                    display:inline-block;
                    padding:5px 5px;
                    .el-alert__icon{font-size:28px;cursor:pointer}
                }
                img {
                    width:100px;
                    height:100px;
                }
            }
        }
        .sola-file-box{
            position:relative;
            .btn-file{
                position:absolute;
                top:0;
                left:0;
                z-index 1;
                opacity:0;
                width :120px;
                height :40px;
                outline: 0;
                background-color :none;
                cursor:pointer;
            }
        }

    }
</style>


